package com.springboot.demo.io.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;

/**
 * 单例channel，起一个过渡产生 MyInHandler
 *
 * @author supu
 * @since 2020-11-17 14:01
 **/
@ChannelHandler.Sharable
public class ChannelInit extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("server registed");

        Channel channel = ctx.channel();
        //2,client::pipeline[ChannelInit,MyInHandler]
        ChannelPipeline pipeline = channel.pipeline();
        // 每次都是新 new MyInHandler
        pipeline.addLast(new MyInHandler());
        // 防止本channel在职责链中重复执行
        ctx.pipeline().remove(this);
    }
}
